﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.DocManger;
using Jzy.Model.DocManger;
using Frame.Lib.Common;
using Frame.Lib.Core.Right;
using Frame.Lib.Core.Dept;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Attach;


namespace Jzy.Web.DocManager.Attach
{
    public partial class Edit : PageEdit
    {
        private string DirID
        {
            get
            {
                return Request["DirID"];
            }
        }

        private DocumentBO _doc;
        private DocumentBO Doc
        {
            get
            {
                if (_doc == null)
                {
                    _doc = new DocumentBO();
                }
                return _doc;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnSave.Attributes.Add("onclick", "showUploadMsg('" + lblMessage.ClientID + "','" + filAttach.ClientID + "');");
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (filAttach.FileName != "")
            {

                DocumentInfo info = Doc.GetInfo(null);
                info.Name = System.IO.Path.GetFileName(filAttach.FileName);
                info.CreatorID = AppSession.UserID;
                info.CreatorName = AppSession.UserName;
                info.InputTime = DateTime.Now;
                info.Orders = Doc.GetMaxOrder();

                info.DirID = int.Parse(this.DirID);
                info.Content = "";

                this.Doc.Save(info);

                //增加默认权限(浏览)
                if (this.PKID == null)
                {
                    int bindID = (int)info.ID;
                    RecordRightBO right = new RecordRightBO("DocRight");
                    DeptBO dept = new DeptBO();
                    int rootID = (int)dept.GetRootID();
                    List<int> lstDeptIDs = new List<int>();
                    lstDeptIDs.Add(rootID);

                    right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Browse, RightOwnerType.Dept);
                    right.SaveOwnerIDs(new List<int>(new int[] { AppSession.UserID }), bindID, RecordRightName.Creator, RightOwnerType.User);
                }

                AttachBO attach = new AttachBO("DocAttach");
                attach.Save(Request.Files, info.ID);
            }
            JSUtil.RefreshView();
        }
    }
}
